package com.dormitory.ui;

import com.dormitory.model.User;
import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame {
    private User currentUser;
    private JPanel contentPanel;
    private CardLayout cardLayout;

    public MainFrame(User user) {
        this.currentUser = user;
        initUI();
    }

    private void initUI() {
        setTitle("宿舍管理系统 - " + currentUser.getUsername());
        setSize(1024, 768);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建菜单栏
        initMenuBar();

        // 创建内容面板
        contentPanel = new JPanel();
        cardLayout = new CardLayout();
        contentPanel.setLayout(cardLayout);
        add(contentPanel);

        // 显示欢迎页面
        showWelcomePanel();
    }

    private void initMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        JMenu moduleMenu = new JMenu("模块");
        JMenuItem studentMenuItem = new JMenuItem("学生信息");
        studentMenuItem.addActionListener(e -> showStudentPanel());
        moduleMenu.add(studentMenuItem);

        JMenuItem buildingMenuItem = new JMenuItem("楼栋信息");
        buildingMenuItem.addActionListener(e -> showBuildingPanel());
        moduleMenu.add(buildingMenuItem);

        JMenuItem roomMenuItem = new JMenuItem("房间信息");
        roomMenuItem.addActionListener(e -> showRoomPanel());
        moduleMenu.add(roomMenuItem);

        JMenuItem checkInMenuItem = new JMenuItem("入住信息");
        checkInMenuItem.addActionListener(e -> showCheckInPanel());
        moduleMenu.add(checkInMenuItem);

        menuBar.add(moduleMenu);
        setJMenuBar(menuBar);
    }

    private void showWelcomePanel() {
        JPanel welcomePanel = new JPanel(new GridBagLayout());
        JLabel welcomeLabel = new JLabel("欢迎使用宿舍管理系统");
        welcomeLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        welcomePanel.add(welcomeLabel);
        
        contentPanel.add(welcomePanel, "welcome");
        cardLayout.show(contentPanel, "welcome");
    }

    private void showStudentPanel() {
        contentPanel.removeAll();
        contentPanel.add(new StudentPanel());
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    private void showBuildingPanel() {
        contentPanel.removeAll();
        contentPanel.add(new BuildingPanel());
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    private void showRoomPanel() {
        contentPanel.removeAll();
        contentPanel.add(new RoomPanel());
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    private void showCheckInPanel() {
        contentPanel.removeAll();
        contentPanel.add(new CheckInPanel());
        contentPanel.revalidate();
        contentPanel.repaint();
    }
} 